package se.webbzon.boltzmann.minigame.effect;

import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxIcon;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxObject;
import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.resourceset.ResourceSet;
import se.webbzon.oschi01.textureset.Texture3D;
import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class SandBoxSun extends DefaultSandBoxItem {
	
	// The 2-dimensional depth of the rain
	private static final int depth = 200;
	
	// The width and height of the sun
	private static final int width = 128, height = 128;
	
	// The texture set of the rain
	private final ResourceSet<Texture3D> textureSet;
	
	// The phase and radius of the sun
	private double dt, t, hradius, vradius;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SandBoxSun() {
		textureSet = Resources.getTextureSet("weather_sun");
		setInstance(new Sun());
		setSize(width,height);
		t = dt = 0;
		hradius = vradius = 0;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the delta time of the sun (radians per step). **/
	public double getDeltaTime() {
		return dt;
	}

	/** Sets the delta time of the sun (radians per step). **/
	public void setDeltaTime(double dt) {
		this.dt = dt;
	}

	/** Returns the time of the sun (Math.PI/2 is noon). **/
	public double getTime() {
		return t;
	}

	/** Sets the time of the sun (Math.PI/2 is noon). **/
	public void setTime(double t) {
		this.t = t;
	}

	/** Returns the horizontal radius of the sun. **/
	public double getHradius() {
		return hradius;
	}

	/** Sets the horizontal radius of the sun. **/
	public void setHradius(double hradius) {
		this.hradius = hradius;
	}

	/** Returns the vertical radius of the sun. **/
	public double getVradius() {
		return vradius;
	}

	/** Sets the vertical radius of the sun. **/
	public void setVradius(double vradius) {
		this.vradius = vradius;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class Sun extends SandBoxObject {

		public Sun() {
			super(WorldObject.onStep + WorldObject.onPostDraw, depth);
		}

		@Override public void onAttach(SandBox box) {};

		@Override public void onDetach(SandBox box) {};

		@Override public SandBoxIcon createIcon() {
			return null;
		}

		@Override public void onStep(WorldArea world) {
			t += dt;
		}

		@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
			int w = (int) getWidth();
			int h = (int) getHeight();
			int x = (int) (getX() - hradius * Math.cos(t)) - w/2;
			int y = (int) (getY() - vradius * Math.sin(t)) - h/2;
			hud.drawImage(textureSet.get(0).getImage(), x, y, w, h);
		}
		
	}

}
